package com.corejsf;

import java.io.Serializable;

//import javax.inject.Inject;
import javax.inject.Inject;
import javax.inject.Named; 
   // or import javax.faces.bean.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.SessionScoped; 
   // or import javax.faces.bean.SessionScoped;

@SuppressWarnings("serial")
@Named
@ConversationScoped
/**
 * Login bean. Validates user login, and creates super user for the 
 * system.
 * @author Jitin Dhillon, Kelton Kowalchuk
 *
 */
public class Login implements Serializable {
	@Inject private Conversation conversation;
	/**
	 * Brings in the credential bean, which stores all the users.
	 */
	@Inject static Credentials credentials;
	/**
	 * Brings in the configurations file which has the default superUser
	 * password and username.
	 */
    @Inject private Configurations configs;
    /**
     * Username variable for input box login srceen.
     */
    String username;
    /**
     * Password variable for input box login screen.
     */
    String password;
    /**
     * The user that is currently logged in and using the system.
     */
    private static User activeUser;

    Login() {
    }

    @PostConstruct
    /**
     * Draws the superUser from the configurations file and creates
     * it in the system if the system is starting up for the first time
     * and there are no users in the system.
     */
    public void instantiate() {
    	//If there are no users in the system, create the super user.
        if(credentials.users.size() == 0){
            String sFullname = configs.getConfig("sFullname");
            String sUsername = configs.getConfig("sUsername");
            String sPassword = configs.getConfig("sPassword");
            int sENum = Integer.parseInt(configs.getConfig("sENum"));
            //Create the administrator user.
            User admin = new User(sFullname, sUsername, sPassword, sENum);
            //Because its the admin, set SuperUser to true;
            admin.setSuperUser(true);
            credentials.users.add(admin);
        }
    }
    /**
     * Validates the user and password input. Set the user to the currentUser.
     * @return returns navigation to the appropriate page. If superuser logged in
     * return navigation to the user modify page.
     */
    public String validate() {
        System.out.println(username + " " + password);
        for(User u : credentials.users) {
            if(u.getUsername().equals(username))
                if(u.getPassword().equals(password)) {
                	conversation.begin();
                    if(u.isSuperUser()){
                    	setActiveUser(u);
                        return "superValid";
                    }else{ 
                    	setActiveUser(u);
                        return "valid";
                }
            }
        }
        return "invalid";
    }
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public static User getActiveUser() {
		return activeUser;
	}

	public void setActiveUser(User activeUser) {
		this.activeUser = activeUser;
	}

	public Conversation getConversation() {
		return conversation;
	}
}